package br.com.tcc.systemdx.persistence.hibernate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.tcc.systemdx.domain.TemperaturaDiaria;
import br.com.tcc.systemdx.persistence.TemperaturaDiariaDAO;

@Repository(TemperaturaDiariaDAO.BEAN_NAME)
@Transactional
public class TemperaturaDiariaHibernateDAOImpl 
				extends AbstractEntityHibernateDAOImpl<TemperaturaDiaria> 
				implements TemperaturaDiariaDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5580005924694359331L;

	public double getAvarageByMonthYear(int month, int year) {
		
		List<TemperaturaDiaria> temperaturasMes = new ArrayList<TemperaturaDiaria>();
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, 1, 0, 0, 0);
		Date inicioMes = calendar.getTime();
		
		if(month == 11) {
			calendar.set(year + 1, 0, 1, 0, 0, 0);
		} else {
			calendar.set(year, month + 1, 1, 0, 0);
		}
		Date fimMes = calendar.getTime();
		
		temperaturasMes = findByCriteria(Restrictions.between("data_ocorrencia", inicioMes, fimMes));
		
		//temperaturasMes = findByCriteria(Restrictions.eq("id", 215));
		//temperaturasMes = findAll();
		
		double media = 0;
		for(int i = 0; i < temperaturasMes.size(); ++i) {
			media += temperaturasMes.get(i).getTemp();
		}

		if(temperaturasMes.size() != 0) media /= temperaturasMes.size();
		
		return media;
	}

}
